import 'package:fl_chart/fl_chart.dart';
import 'package:flutter/material.dart';

/// @Title   饼状图页面-环形图
/// @Author: bryce
/// @CreateDate:  2023年08月21日20:26:53
class ChartPieWidget extends StatefulWidget {
  final List<double> values;
  final List<Color> colors;
  final Widget Function(int index) buildCenterWidget;
  final double? width, pieWidth, touchedPieWidth, sectionsSpace;
  final int defaultIndex;
  final Function(int index)? touchCallback;

  const ChartPieWidget({super.key,
    this.width,
    this.pieWidth = 10.0,
    this.touchedPieWidth = 20.0,
    this.sectionsSpace = 2.0,
    required this.values,
    required this.colors,
    required this.buildCenterWidget,
    this.touchCallback,
    this.defaultIndex = 0
  });

  @override
  State<StatefulWidget> createState() => _ChartPieWidgetState();
}

class _ChartPieWidgetState extends State<ChartPieWidget> {
  int touchedIndex = 0;

  // double pieWidth = 10.0, touchedPieWidth = 20.0;
  double centerSpaceRadius = 100;

  // 是否数值全是0
  bool isAllZero = false;

  @override
  void initState() {
    super.initState();
    touchedIndex = widget.defaultIndex;
  }

  @override
  void didUpdateWidget(covariant ChartPieWidget oldWidget) {
    super.didUpdateWidget(oldWidget);
    touchedIndex = widget.defaultIndex;
  }

  @override
  Widget build(BuildContext context) {
    isAllZero = widget.values.reduce((a, b) => a + b) == 0;
    double width = widget.width ?? (MediaQuery
        .of(context)
        .size
        .width - 96);
    return ClipOval(
        child: SizedBox(
            width: width,
            height: width,
            child: Stack(
              children: [
                PieChart(
                  PieChartData(
                    pieTouchData: PieTouchData(
                      touchCallback: (FlTouchEvent event, pieTouchResponse) {
                        if (null != pieTouchResponse) {
                          int eventSectionIndex = pieTouchResponse
                              .touchedSection!.touchedSectionIndex;
                          if (eventSectionIndex > -1) {
                            touchedIndex = eventSectionIndex;
                            setState(() {});
                            widget.touchCallback?.call(touchedIndex);
                          }
                        }
                      },
                    ),
                    borderData: FlBorderData(show: false),
                    sectionsSpace: widget.sectionsSpace ?? 2,
                    centerSpaceRadius: (width -
                        (widget.touchedPieWidth ?? 20.0) * 2) / 2,
                    startDegreeOffset: 270,
                    sections: showingSections(),
                  ),
                ),
                Center(child: widget.buildCenterWidget(touchedIndex))
              ],
            )));
  }

  List<PieChartSectionData> showingSections() {
    return widget.values
        .asMap()
        .entries
        .map((e) {
      final isTouched = e.key == touchedIndex;
      final radius = isTouched ? widget.touchedPieWidth : widget.pieWidth;

      return PieChartSectionData(
          color: widget.colors[e.key],
          value: isAllZero ? 1 : e.value,
          showTitle: false,
          radius: radius);
    }).toList();
  }
}
